package com.manor.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import com.manor.common.R;
import com.manor.common.custom.avi.AVLoadingIndicatorView;

public class WaitingDialog extends Dialog {
    private AVLoadingIndicatorView avLoad = null;
    Context mContext;

    //private final TextView mTvMessage;

    public WaitingDialog(@NonNull Context context) {
        super(context, R.style.WaitingDialog);

        setContentView(R.layout.dialog_waiting);
        mContext =context;
       initView();
        //int size = (int) (getScreenWidth(context) * 0.33);
        //getWindow().setLayout(size, size);


    }

    private void initView(){
        avLoad = findViewById(R.id.avLoad);
        avLoad.setIndicator("LineScaleIndicator");
        avLoad.setIndicatorColor(Color.parseColor("#FF1BF8E6"));
    }

    @Override
    public void dismiss() {
        if (avLoad!=null) {
            avLoad.hide();
        }
        super.dismiss();
    }

    @Override
    public void show() {
        super.show();
        avLoad.show();
    }

    public static WaitingDialog newDialog(Context context) {
        return new WaitingDialog(context);
    }



    /**
     * 设置对话框占屏幕宽度的比例
     *
     * @param rate
     * @return
     */
    public WaitingDialog setSizeRate(float rate) {

        if (rate < 0.3f || rate > 0.5f) {
            rate = 0.33f;
        }
        int size = (int) (getScreenWidth(getContext()) * rate);
        getWindow().setLayout(size, size);
        return this;
    }

    private static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}
